//
//  HUD_SingleCase.m
//  ssy
//
//  Created by 李志权 on 15/6/18.
//  Copyright (c) 2015年 深圳时时医科技有限公司. All rights reserved.
//

#import "HUD_SingleCase.h"

@implementation HUD_SingleCase

static HUD_SingleCase* s_HUD;

/** MBProgressHUD单例*/
+ (HUD_SingleCase*)HUDALLOW
{
    //@synchronized 防止多个线程同时访问
    @synchronized(self) {
        if (s_HUD == nil) {
            s_HUD = [[HUD_SingleCase alloc] init];
        }
    }
    return s_HUD;
}

- (UIView*)HUDTextDialog:(NSString*)message interval:(int)interval success:(void (^)())success
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((Width-200)/2, (Height-200-64-64)/2, 200, 200)];
    
    //初始化进度框，置于当前的View当中
    self.HUD = [[MBProgressHUD alloc] initWithView:view];
    
//    NSLog(@"%f",self.HUD.bounds.size.width);
    [view addSubview:self.HUD];
    
    self.HUD.labelText = message;//设置对话框文字
    self.HUD.mode = MBProgressHUDModeIndeterminate;//设置模式为等待提示框
    
    //显示对话框
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(interval);
    }completionBlock:^{
        
        if (success)
        {
            success();
        }
        
        //操作执行完后取消对话框
        [self.HUD removeFromSuperview];
        self.HUD = nil;
    }];
    
    return view;
}

- (UIView*)HUDDialogUntil:(NSString*)message
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((Width-200)/2, (Height-200-64-64)/2, 200, 200)];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:view];
    self.HUD.labelText = message;//设置对话框文字
    self.HUD.mode = MBProgressHUDModeIndeterminate;//设置模式为等待提示框
    [self.HUD show:YES];//显示等待提示框
    [view addSubview:self.HUD];
    
    return view;
}

- (UIView*)HUDDialogWithViewUntil:(NSString*)message pview:(UIView*)pview
{
    CGRect frame = pview.frame;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((frame.size.width-200)/2, (frame.size.height-200)/2, 200, 200)];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:view];
    self.HUD.labelText = message;//设置对话框文字
    self.HUD.mode = MBProgressHUDModeIndeterminate;//设置模式为等待提示框
    [self.HUD show:YES];//显示等待提示框
    [view addSubview:self.HUD];
    
    return view;
}


- (UIView*)HUDAllTextDialog:(NSString*)message interval:(int)interval success:(void (^)())success
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((Width-200)/2, (Height-200-64-64)/2, 200, 200)];
    
    //初始化进度框，置于当前的View当中
    self.HUD = [[MBProgressHUD alloc] initWithView:view];
    
//    NSLog(@"%f",self.HUD.bounds.size.width);
    [view addSubview:self.HUD];
    
    self.HUD.labelText = message;//设置对话框文字
    self.HUD.mode = MBProgressHUDModeText;//设置模式为纯文本
    
    //显示对话框
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(interval);
    }completionBlock:^{
        
        if (success)
        {
            success();
        }
        
        //操作执行完后取消对话框
        [self.HUD removeFromSuperview];
        self.HUD = nil;
    }];
    
    return view;
}

@end
